package cytj.setup.fragment;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import cytj.setup.R;
import cytj.setup.activity.SetupActivity;

/**
 * 数据接收的Fragment隶属于SetupActivity
 * Created by LTP on 2015/12/28
 */
public class DataReceiveFragment extends Fragment implements AdapterView.OnItemLongClickListener {
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_data_receive, container, false);
        initView();
        return view;
    }

    /**
     * 控件初始化
     */
    private void initView() {
        ListView listView_dataReceive = (ListView) view.findViewById(R.id.listView_dataReceive_dataReceiveFragment);
        listView_dataReceive.setAdapter(((SetupActivity) getActivity()).adapter);
        listView_dataReceive.setOnItemLongClickListener(this);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        // 长按弹出"清空"的菜单项，点击后清空
        new AlertDialog.Builder(getActivity()).setItems(new String[]{"清空"}, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((SetupActivity) getActivity()).dataList.clear();
                ((SetupActivity) getActivity()).adapter.notifyDataSetChanged();
            }
        }).show();
        return false;
    }
}
